package hu.kisprof.quizServer.controller;

import hu.kisprof.quiz.model.bean.Player;
import hu.kisprof.quiz.model.bean.Topic;

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.TreeSet;

public class QuizServerIncomingConnection extends Thread{
	private static int MAXCONNECTION=1000;
	private Socket incoming;
	private QuizServerController control;
	private int type;
	
	
	public QuizServerIncomingConnection(Socket incoming, QuizServerController control ){
		this.incoming = incoming;
		this.control = control;
	}
	
	public void run(){
		
		try {
			System.out.println("New QuizServerConnection"+incoming);
			DataOutputStream dos = new DataOutputStream(incoming.getOutputStream());
			DataInputStream dis = new DataInputStream(incoming.getInputStream());
			ObjectOutputStream oos = new ObjectOutputStream(incoming.getOutputStream());
			ObjectInputStream ois = new ObjectInputStream(incoming.getInputStream());
			
			if(--MAXCONNECTION<0){
				dos.writeBoolean(false);
				System.out.println("   MaxConnection reached -> Connection refused");
			}else{
				dos.writeBoolean(true);
				
				type = dis.readInt();
				switch (type) {
					case 0:
						break;
					case 1:
						System.out.println("   1:getRooms()");
						oos.writeObject(control.getRooms());
						break;
					case 2:
						System.out.println("   2:createRoom(String)");
						dos.writeBoolean(control.createRoom(dis.readUTF()));
						break;
					case 3:
						System.out.println("   3:getRoomPlayers(String)");
						oos.writeObject(control.getRoomPlayers(dis.readUTF()));
						break;
					case 4:
						System.out.println("   4:isPlayerExist(String)");
						dos.writeBoolean(control.isPlayerExist(dis.readUTF()));
						break;
					case 5:
						System.out.println("   5:registerPlayer(Player)");
						dos.writeBoolean(control.registerPlayer((Player)ois.readObject()));
						break;
					case 6:
						System.out.println("   6:login(String,String)");
						oos.writeObject(control.login(dis.readUTF(), dis.readUTF(),incoming.getInetAddress(),dis.readInt()));
						break;
					case 7:
						System.out.println("   7:join room (String,Player)");
						dos.writeBoolean(control.joinRoom(dis.readUTF(), (Player) ois.readObject()));
						break;
					case 8:
						System.out.println("   8:leave room (String, Player)");
						control.leaveRoom(dis.readUTF(), (Player) ois.readObject());
						break;
					case 9:
						System.out.println("   9:challange player (String,String)");
						control.challangePlayer(dis.readUTF(),dis.readUTF(),dis.readUTF(),dis.readInt());
						break;
					case 10:
						System.out.println("  10:isPlayerOnline(String)");
						dos.writeBoolean(control.isPlayerOnline(dis.readUTF()));
						break;
					case 11:
						System.out.println("  11:logoff(String)");
						control.logout(dis.readUTF());
						break;
					case 12:
						System.out.println("  12:playerReady(String)");
						control.playerReady(dis.readUTF(),dis.readBoolean());
						break;
					case 13:
						System.out.println("  13:playerStatistic(String)");
						oos.writeObject(control.playerStatistic(dis.readUTF()));
						break;
					case 14:
						System.out.println("   14:getQuestions()");
						oos.writeObject(control.getQuestions());
						break;
					case 15:
						System.out.println("   15:getTopics()");
						oos.writeObject(control.getTopics());
						break;
					case 16:
						System.out.println("   16:addTopic(String)");
						control.addTopic(dis.readUTF());
						break;
					case 17:
						System.out.println("   17:addQuestion(String,String,TreeSet<Topic>,int,TreeSet<String>)");
						control.addQuestion(dis.readUTF(),dis.readUTF(),(TreeSet<Topic>)ois.readObject(),dis.readInt(),(TreeSet<String>)ois.readObject());
						break;
					case 18:
						System.out.println("   18:getEdus()");
						oos.writeObject(control.getEdus());
						break;
					case 19:
						System.out.println("   19:checkZipCode(int)");
						dos.writeUTF(control.checkZipCode(dis.readInt()));
						break;
					case 20:
						System.out.println("   20:deleteQuestion(int)");
						control.deleteQuestion(dis.readInt());
						break;
					case 21:
						System.out.println("   21:editQuestion(int)");
						control.editQuestion(dis.readInt(), dis.readUTF(), dis.readUTF(), (TreeSet<Topic>)ois.readObject(), dis.readInt(), (TreeSet<String>)ois.readObject());
						break;
					case 22:
						System.out.println("   22:getQuestion(int)");
						oos.writeObject(control.getQuestion(dis.readInt()));
						break;
					case 23:
						System.out.println("   23:getPoints(String)");
						dos.writeInt(control.getPoints(dis.readUTF()));
						break;
					case 24:
						System.out.println("   24:getStatistic(int)");
						oos.writeObject(control.getStatistic(dis.readInt()));
						break;
					case 25:
						System.out.println("   25:getAllPlayers()");
						oos.writeObject(control.getAllPlayers());
						break;
					case 26:
						System.out.println("   26:setAdmin(String)");
						control.setAdmin(dis.readUTF());
						break;
					case 27:
						System.out.println("   27:deletePlayer(String)");
						control.deletePlayer(dis.readUTF());
						break;
						
						
						
						

				}
				
			}
			ois.close();
			oos.close();
			dis.close();
			dos.close();
			
			incoming.close();
			MAXCONNECTION++;
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
}
